import { _decorator, Color, Component, Node, Sprite, SpriteFrame, Vec3 } from 'cc';
import { SpriteFramesCfg } from '../../../auto/SpriteFramesCfg';
const { ccclass, property } = _decorator;

enum MovingType {
    /**无动画 */
    none = 0,
    /**从棋盘飞到收集区 */
    flying,
    /**收集区调整位置 */
    adjusting
}

@ccclass('Match3ZiUE')
export class Match3ZiUE extends Component {

    /**棋子白板 */
    @property(Sprite)
    private bg: Sprite = null;

    /**棋子表面花色 */
    @property(Sprite)
    private sprite: Sprite = null;

    public col: number = 0;
    public row: number = 0;
    public style: number = 0;

    /**点击棋子方法 */
    private m_ClickFunc: (zi: Match3ZiUE) => any = null;

    /**移动类型 */
    private m_MovingType: MovingType = MovingType.none;
    /**移动到哪个目标 */
    private m_TargetPosition: Vec3 = new Vec3(0, 0, 0);
    /**用平滑因子替换，二维移动速度 */
    private m_SmoothFactor: number = 0.2;
    /**移动回调 */
    private m_FlyingCallBack: Function = null;

    /**消除标记 */
    public isMarkElimnate: boolean = false;

    /**
     * 初始化
     * @param col 列
     * @param row 行
     * @param style 样式
     */
    public init(col: number, row: number, style: number, clickFunc: (zi: Match3ZiUE) => any) {
        this.col = col;
        this.row = row;
        this.style = style;
        this.m_ClickFunc = clickFunc;
        this.node.setPosition(col * 17, row * 20);
        this.setDisplay(style);
    }

    click() {
        this.m_ClickFunc(this);
    }

    /**设置牌面花色 */
    public setDisplay(style: number) {
        let spriteFrame = gCtrl.res.getAsset(SpriteFramesCfg.pai(`pai-${style}`), SpriteFrame);
        this.sprite.spriteFrame = spriteFrame;
    }

    /**设置是否可以点击(不可点击的时候变暗) */
    public setClickable(bClickable: boolean) {
        this.bg.color = bClickable ? Color.WHITE : new Color(120, 120, 120, 180);
        this.sprite.color = bClickable ? Color.WHITE : new Color(120, 120, 120, 180);
    }

    /**移动到指定位置,调用此方法时，一定是从棋盘移动到收集区 */
    public moveToTargetIndex(collectArea: Node, index: number, cb: Function) {
        if (this.m_MovingType !== MovingType.none) {
            return;
        }
        this.m_MovingType = MovingType.flying;
        this.m_FlyingCallBack = cb;
        //目前表现是移动到collectArea节点下
        this.node.setParent(collectArea, true);
        //设置运动目标
        this.m_TargetPosition.set(index * 102, 0, 0);
        //开启调度控制飞行
        this.schedule(this.updateMoving, 0);
    }

    /**移动调度 */
    private updateMoving(dt: number) {
        let pos = this.node.position;
        //到达目标位置
        if (Math.abs(this.m_TargetPosition.x - pos.x) + Math.abs(this.m_TargetPosition.y - pos.y) < this.m_SmoothFactor) {
            this.node.setPosition(this.m_TargetPosition.x, this.m_TargetPosition.y, pos.z);
            this.unschedule(this.updateMoving);
            let movingType = this.m_MovingType;
            this.m_MovingType = MovingType.none;
            if (movingType === MovingType.flying) {
                let flyingCallBack = this.m_FlyingCallBack;
                this.m_FlyingCallBack = null;
                return flyingCallBack();
            } else {
                return;
            }
        }
        this.node.setPosition(new Vec3(pos.x + (this.m_TargetPosition.x - pos.x) * this.m_SmoothFactor, pos.y + (this.m_TargetPosition.y - pos.y) * this.m_SmoothFactor, pos.z));
    }

    /**中途更改目标位置 */
    public changeMoveingTargetIndex(index: number): void {
        //设置运动目标位置
        this.m_TargetPosition.set(index * 102, 0, 0);
        switch (this.m_MovingType) {
            case MovingType.none:
                //说明是收牌调整动画开启
                this.m_MovingType = MovingType.adjusting;
                //开启调度控制移动
                this.schedule(this.updateMoving, 0);
                break;
            case MovingType.adjusting:
            case MovingType.flying:
                //在调整动画或者飞行的时候，又修改一次目标位置->改目标位置即可(上方已实现)
                break;
        }
    }

}


